﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.Customer;
using SystemCenterSoa.IRepository.CustomerManger;
using SystemCenterSoa.PoModel.Customer;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.CustomerManger
{
    public class CustomerRepository : BaseRepository, CustomerIRepository
    {

        public CustomerRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }
        /// <summary>
        /// 客户基本信息扩展信息添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool CustomerAdd(CustomerPo info)
        {
            info.cust_create_time = DateTime.Now;
            info.cust_create_people = "gzb";
            info.cust_update_time = DateTime.Now;
            _systemCenterDbSoaContext.CustomerPo.Add(info);
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

       public bool CustomerUpdate(CustomerPo info)
        {
            var list = _systemCenterDbSoaContext.CustomerPo.FirstOrDefault(c => c.customer_id == info.customer_id);
            list.customer_for_short = info.customer_for_short;
            list.customer_fax_number = info.customer_fax_number;
            list.customer_company_tel = info.customer_company_tel;
            list.customer_company_address = info.customer_company_address;
            list.customer_client_name = info.customer_client_name;
            list.client_id = info.client_id;
            list.city_id = info.city_id;
            list.customer_linkman = info.customer_linkman;
            list.customer_number = info.customer_number;
            list.customer_postal_code = info.customer_postal_code;
            list.enterprise_code = info.enterprise_code;
            list.extend_bank_account = info.extend_bank_account;
            list.extend_bank_name = info.extend_bank_name;
            list.extend_client_english_name = info.extend_client_english_name;
            list.extend_credit_ratings_id = info.extend_credit_ratings_id;
            list.extend_email = info.extend_email;
            list.extend_homepage_of_a_company = info.extend_homepage_of_a_company;
            list.extend_intimacy_id = info.extend_intimacy_id;
            list.extend_legal_published = info.extend_legal_published;
            list.extend_relation_department = info.extend_relation_department;
            list.extend_relation_time = info.extend_relation_time;
            list.extend_remark = info.extend_remark;
            list.extend_taxpayer_registration_number = info.extend_taxpayer_registration_number;
            list.extenf_embranchment = info.extenf_embranchment;
            list.industry_id = info.industry_id;
            list.province_id = info.province_id;
            list.state_id = info.state_id;
            list.whether_cooperation_id = info.whether_cooperation_id;
            list.cust_update_time = DateTime.Now;
            list.cust_update_people = "gzb";
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }
        /// <summary>
        /// 甲方信息添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool FirstPartyAdd(FirstPartyPo info)
        {
            
            _systemCenterDbSoaContext.FirstPartyPo.Add(info);
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }
        /// <summary>
        /// 满意度调查信息添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool SatisfactionSurveyAdd(SatisfactionSurveyPo info)
        {
            info.satis_create_people = "gzb";
            info.satis_create_time = DateTime.Now;
            info.satis_update_time = DateTime.Now;
            _systemCenterDbSoaContext.SatisfactionSurveyPo.Add(info);
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        //public List<StateCityPo> GetStateCity(int id)
        //{
        //    var list = _systemCenterDbSoaContext.StateCityPo.Where(c => c.state_city_lid == id).ToList();
        //    return list;
        //}

        public List<FirstPartyPo> GetFirstParty()
        {
            var list = _systemCenterDbSoaContext.FirstPartyPo.ToList();
            int n = list.Count();
            return _systemCenterDbSoaContext.FirstPartyPo.Where(c => c.first_party_id == n).ToList();
        }
        /// <summary>
        /// 获取调查满意度最后一条数据
        /// </summary>
        /// <returns></returns>
        public List<SatisfactionSurveyPo> GetSatisfactionSurveyPos()
        {
            var list = _systemCenterDbSoaContext.SatisfactionSurveyPo.ToList();
            int n = list.Count();
            list = list.Where(c => c.satisfaction_survey_id == n).ToList();
            return list.ToList();
        }
        
        public FirstPartyPo FanFirst(int id)
        {
            return _systemCenterDbSoaContext.FirstPartyPo.FirstOrDefault(c => c.first_party_id == id);
        }

        public bool DeleFirst(int id)
        {
            var list = _systemCenterDbSoaContext.FirstPartyPo.Find(id);
            _systemCenterDbSoaContext.Remove(list);
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        public bool UpdateFirst(FirstPartyPo info)
        {
            var list = _systemCenterDbSoaContext.FirstPartyPo.FirstOrDefault(c => c.first_party_id == info.first_party_id);
            list.first_party_number = info.first_party_number;
            list.first_party_name = info.first_party_name;
            list.first_party_duty = info.first_party_duty;
            list.first_party_department = info.first_party_department;
            list.first_party_tell = info.first_party_tell;
            list.first_party_business_calls = info.first_party_business_calls;
            list.first_party_homephone = info.first_party_homephone;
            list.first_party_business_fax = info.first_party_business_fax;
            list.first_party_homefax = info.first_party_homefax;
            list.first_party_email = info.first_party_email;
            list.first_party_remark = info.first_party_remark;
            list.first_update_people = "gzb";
            list.first_update_time = DateTime.Now;
            int n = _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        public SatisfactionSurveyPo ManCha(int id)
        {
            return _systemCenterDbSoaContext.SatisfactionSurveyPo.FirstOrDefault(c => c.satisfaction_survey_id == id);
        }

        public bool Deleman(int id)
        {
            var list = _systemCenterDbSoaContext.SatisfactionSurveyPo.Find(id);
            _systemCenterDbSoaContext.SatisfactionSurveyPo.Remove(list);
             _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        /// <summary>
        /// 满意度修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool Updateman(SatisfactionSurveyPo info)
        {
            var list = _systemCenterDbSoaContext.SatisfactionSurveyPo.FirstOrDefault(c => c.satisfaction_survey_id == info.satisfaction_survey_id);
            list.satisfaction_survey_id = info.satisfaction_survey_id;
            list.satisfaction_survey_number = info.satisfaction_survey_number;
            list.satisfaction_survey_content = info.satisfaction_survey_content;
            list.satisfaction_survey_time = info.satisfaction_survey_time;
            list.satisfaction_id = info.satisfaction_id;
            list.satisfaction_cause = info.satisfaction_cause;
            list.satisfaction_type_id = info.satisfaction_type_id;
            list.most_satisfying_aspect = info.most_satisfying_aspect;
            list.most_usatisfying_aspect = info.most_usatisfying_aspect;
            list.satisfaction_remark = info.satisfaction_remark;
            list.satis_update_time = DateTime.Now;
            list.satis_update_people = "gzb";
           _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        public List<China> GetChina(int id)
        {
            var list = _systemCenterDbSoaContext.China.Where(c => c.Pid == id);
            return list.ToList();
        }

        public PagerResult<CustomerDto> GetAllInfo(SearchModel searchModel)
        {
            var list = from a in _systemCenterDbSoaContext.CustomerPo
                       join b in _systemCenterDbSoaContext.FirstPartyPo
                       on a.customer_number equals b.first_party_number
                       join c in _systemCenterDbSoaContext.SatisfactionSurveyPo
                       on a.customer_number equals c.satisfaction_survey_number
                       join d in _systemCenterDbSoaContext.China
                       on a.state_id equals d.Id
                       join e in _systemCenterDbSoaContext.China
                       on a.province_id equals e.Id
                       join f in _systemCenterDbSoaContext.China
                       on a.city_id equals f.Id
                       select new CustomerDto
                       {
                           cust_update_time=a.cust_update_time,
                           cust_create_people=a.cust_create_people,
                           cust_create_time=a.cust_create_time,
                           cust_update_people=a.cust_update_people,
                           first_create_people=b.first_create_people,
                           first_create_time=b.first_create_time,
                           first_update_people=b.first_update_people,
                           first_update_time=b.first_update_time,
                           satis_create_people=c.satis_create_people,
                           satis_create_time=c.satis_create_time,
                           satis_update_people=c.satis_update_people,
                           satis_update_time=c.satis_update_time,
                           cityname=f.Name,
                           provincename=e.Name,
                           statename=d.Name,
                           customer_client_name=a.customer_client_name,
                           customer_company_address=a.customer_company_address,
                           customer_number=a.customer_number,
                           customer_company_tel=a.customer_company_tel,
                           customer_fax_number=a.customer_fax_number,
                           customer_for_short=a.customer_for_short,
                           customer_id=a.customer_id,
                           customer_linkman=a.customer_linkman,
                           customer_postal_code=a.customer_postal_code,
                           enterprise_code=a.enterprise_code,
                           extend_bank_account=a.extend_bank_account,
                           extend_bank_name=a.extend_bank_name,
                           extend_client_english_name=a.extend_client_english_name,
                           extend_credit_ratings_id=a.extend_credit_ratings_id,
                           extend_email=a.extend_email,
                           extend_homepage_of_a_company=a.extend_homepage_of_a_company,
                           extend_intimacy_id=a.extend_intimacy_id,
                           extend_legal_published=a.extend_legal_published,
                           extend_relation_department=a.extend_relation_department,
                           extend_relation_time=a.extend_relation_time,
                           extend_remark=a.extend_remark,
                           extend_taxpayer_registration_number=a.extend_taxpayer_registration_number,
                           extenf_embranchment=a.extenf_embranchment,
                           city_id=a.city_id,
                           state_id=a.state_id,
                           province_id=a.province_id,
                           first_party_business_calls=b.first_party_business_calls,
                           first_party_business_fax=b.first_party_business_fax,
                           first_party_department=b.first_party_department,
                           first_party_duty=b.first_party_duty,
                           first_party_email=b.first_party_email,
                           first_party_homefax=b.first_party_homefax,
                           first_party_homephone=b.first_party_homephone,
                           first_party_id=b.first_party_id,
                           first_party_name=b.first_party_name,
                           first_party_number=b.first_party_number,
                           first_party_remark=b.first_party_remark,
                           first_party_tell=b.first_party_tell,
                           satisfaction_cause=c.satisfaction_cause,
                           satisfaction_id=c.satisfaction_id,
                           satisfaction_remark=c.satisfaction_remark,
                           satisfaction_survey_content=c.satisfaction_survey_content,
                           satisfaction_survey_id=c.satisfaction_survey_id,
                           satisfaction_survey_number=c.satisfaction_survey_number,
                           satisfaction_survey_time=c.satisfaction_survey_time,
                           satisfaction_type_id=c.satisfaction_type_id,
                           most_satisfying_aspect=c.most_satisfying_aspect,
                           client_id=a.client_id,
                           industry_id=a.industry_id,
                           most_usatisfying_aspect=c.most_usatisfying_aspect,
                           whether_cooperation_id=a.whether_cooperation_id
                       };
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                Dictionary<string, string> tempDi = searchModel.SearchItem;
                string name = "name";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(name) && !string.IsNullOrEmpty(tempDi[name]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_client_name.Contains(tempDi[name]));
                }

                string bh = "bh";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(bh) && !string.IsNullOrEmpty(tempDi[bh]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_number==(tempDi[bh]));
                }

                string lxr = "lxr";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(lxr) && !string.IsNullOrEmpty(tempDi[lxr]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_linkman == (tempDi[lxr]));
                }

                string lxdh = "lxdh";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(lxdh) && !string.IsNullOrEmpty(tempDi[lxdh]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_company_tel.Contains(tempDi[lxdh]));
                }

                string companyadd = "companyadd";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(companyadd) && !string.IsNullOrEmpty(tempDi[companyadd]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_company_address.Contains(tempDi[companyadd]));
                }

                string youbm = "youbm";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(companyadd) && !string.IsNullOrEmpty(tempDi[youbm]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_postal_code.Contains(tempDi[youbm]));
                }

                string faxnumber = "faxnumber";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(faxnumber) && !string.IsNullOrEmpty(tempDi[faxnumber]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_fax_number.Contains(tempDi[faxnumber]));
                }

                string customerjc = "customerjc";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(customerjc) && !string.IsNullOrEmpty(tempDi[customerjc]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.customer_for_short.Contains(tempDi[customerjc]));
                }

                string customeryname = "customeryname";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(customeryname) && !string.IsNullOrEmpty(tempDi[customeryname]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.extend_client_english_name.Contains(tempDi[customeryname]));
                }

                string lxname = "lxname";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(lxname) && !string.IsNullOrEmpty(tempDi[lxname]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.first_party_name.Contains(tempDi[lxname]));
                }

                string state_id = "state_id";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(state_id) && !string.IsNullOrEmpty(tempDi[state_id]))
                {
                    int state = -1;
                    if (int.TryParse(tempDi[state_id], out state))
                    {
                        list = list.Where(c => c.state_id == state);
                    }

                }

                string province_id = "province_id";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(province_id) && !string.IsNullOrEmpty(tempDi[province_id]))
                {
                    int province = -1;
                    if (int.TryParse(tempDi[province_id], out province))
                    {
                        list = list.Where(c => c.province_id == province);
                    }

                }

                string city_id = "city_id";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(city_id) && !string.IsNullOrEmpty(tempDi[city_id]))
                {
                    int city = -1;
                    if (int.TryParse(tempDi[city_id], out city))
                    {
                        list = list.Where(c => c.city_id == city);
                    }

                }


                string companyzhu = "companyzhu";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(companyzhu) && !string.IsNullOrEmpty(tempDi[companyzhu]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.extend_homepage_of_a_company.Contains(tempDi[companyzhu]));
                }

                string accountname = "accountname";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(accountname) && !string.IsNullOrEmpty(tempDi[accountname]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.extend_bank_name.Contains(tempDi[accountname]));
                }

                string account = "account";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(account) && !string.IsNullOrEmpty(tempDi[account]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.extend_bank_account.Contains(tempDi[account]));
                }

                string enterprisecode = "enterprisecode";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(enterprisecode) && !string.IsNullOrEmpty(tempDi[enterprisecode]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.enterprise_code.Contains(tempDi[enterprisecode]));
                }

                string published = "published";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(published) && !string.IsNullOrEmpty(tempDi[published]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.extend_legal_published.Contains(tempDi[published]));
                }

                string registrationnumber = "registrationnumber";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(registrationnumber) && !string.IsNullOrEmpty(tempDi[registrationnumber]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.extend_taxpayer_registration_number.Contains(tempDi[registrationnumber]));
                }

                string startfirsttime = "startfirsttime";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(startfirsttime) && !string.IsNullOrEmpty(tempDi[startfirsttime]))
                {
                    DateTime tempCreatorDate = new DateTime();
                    if (DateTime.TryParse(tempDi[startfirsttime], out tempCreatorDate))
                    {
                        list = list.Where(c => c.first_create_time >= tempCreatorDate);
                    }

                }

                string endfirsttime = "endfirsttime";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(endfirsttime) && !string.IsNullOrEmpty(tempDi[endfirsttime]))
                {
                    DateTime tempCreatorDate = new DateTime();
                    if (DateTime.TryParse(tempDi[endfirsttime], out tempCreatorDate))
                    {
                        list = list.Where(c => c.first_create_time <= tempCreatorDate);
                    }

                }
            }
            return BasePageSearch(list, searchModel);
        }

        public List<CustomerPo> Tian(int id)
        {
            var list = _systemCenterDbSoaContext.CustomerPo.ToList();
            list = list.Where(c => c.customer_id == id).ToList();
            return list;
        }

        public CustomerPo FanCustomer(int id)
        {
            var list = _systemCenterDbSoaContext.CustomerPo.Find(id);
            return list;
        }

        public List<FirstPartyPo> Fanjia(int id)
        {
            var list = _systemCenterDbSoaContext.FirstPartyPo.Where(c => c.first_party_id == id).ToList();
            return list;
        }

        public List<SatisfactionSurveyPo> Fanman(int id)
        {
            var list = _systemCenterDbSoaContext.SatisfactionSurveyPo.Where(c => c.satisfaction_survey_id == id).ToList();
            return list;
        }

        public bool Dele(string ids)
        {
            string[] arrid = ids.Split(',');
            foreach (var item in arrid)
            {

                var list=_systemCenterDbSoaContext.CustomerPo.FirstOrDefault(c => c.customer_number == item);
                var list2 = _systemCenterDbSoaContext.FirstPartyPo.FirstOrDefault(c => c.first_party_number == item);
                var list3 = _systemCenterDbSoaContext.SatisfactionSurveyPo.FirstOrDefault(c => c.satisfaction_survey_number == item);
                _systemCenterDbSoaContext.Remove(list);
                _systemCenterDbSoaContext.Remove(list2);
                _systemCenterDbSoaContext.Remove(list3);
            }
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }
    }
}
